import {observable, action, runInAction} from "mobx";
import {getTamperDatas, checkProtectDatas} from "../apis/secondAuth";
import {WeaLocaleProvider} from "ecCom";

const getLabel = WeaLocaleProvider.getLabel;

export default class SecondAuthStore {
    /**
     * 验证码的值
     */
    authCode = "";

    /**
     * CA认证的key
     */
    authKey = "";

    /**
     * 是否是数据保护的验证窗口
     */
    isProtectDialog = 0;

    /**
     * 二次认证的类型
     */
    @observable
    secondAuthType = 0;

    /**
     * 签字意见的id
     */
    logId = -1;

    /**
     *  流程的一些参数
     */
    commonParam = {};

    /**
     * 查看篡改信息详情窗口的tab
     */
    @observable
    tabs = [];

    /**
     * tab被选中的key
     */
    @observable
    selectedKey = "";

    /**
     * 证书信息tab页的数据
     */
    @observable
    certInfo = [];

    /**
     * 篡改信息tab页的数据
     */
    @observable
    tamperInfo = {};

    /**
     *  二次验证的结果
     */
    @observable
    checkResult = mobx.observable.map({});

    @observable
    isProtected = mobx.observable.map({});

    @observable
    protectTypes = mobx.observable.map({});

    @observable
    signUrl = "";

    @observable
    APIResult = {};

    @action("设置验证码的值")
    saveAuthCode(acode) {
        this.authCode = acode;
    }

    @action("设置CA认证的信息")
    setAuthData(data) {
        /*
         this.authKey = data.authKey || "";
         this.secondAuthType = data.secondAuthType || 0;
         this.isProtectDialog = data.isProtectDialog || 0;
         */

        /**
         * 设置authKey,secondAuthType,isProtectDialog,signUrl的值
         */
        Object.assign(this, data);
    }

    @action("加载篡改的详细信息")
    loadAuthDetail() {
        this.tabs = [];
        this.selectedKey = "tamperInfo";

        const {requestid, workflowid, nodeid, formid, isbill} = this.commonParam;
        const logid = this.logId;
        const requestParam = {requestid, workflowid, nodeid, formid, isbill, logid};

        getTamperDatas(requestParam).then(data => {
            const {success} = data;

            runInAction(() => {
                this.APIResult = data;

                if (success == "1") {
                    const {certInfo, tamperInfo} = data;
                    const {checkResult = 0} = tamperInfo;
                    let title = getLabel(501477, "篡改信息");
                    if (checkResult === "1") {
                        title = getLabel(506277, "受保护数据");
                    }

                    this.certInfo = [...certInfo];
                    if (certInfo.length > 0) {
                        this.tabs.push({tabKey: "certInfo", title: getLabel(501463, "证书信息")});
                    }

                    this.tamperInfo = mobx.observable.map(tamperInfo);
                    this.tabs.push({tabKey: "tamperInfo", title});
                }
            });
        });
    }

    @action("验证保护的数据是否被篡改")
    checkProtectDatas() {
        this.tabs = [];
        this.selectedKey = "tamperInfo";

        const {requestid, workflowid, nodeid, formid, isbill} = this.commonParam;
        const logId = this.logId;
        const requestParam = {
            requestId: requestid,
            workflowId: workflowid,
            nodeId: nodeid,
            formId: formid,
            isbill,
            logId
        };

        return checkProtectDatas(requestParam);
    }

    @action("切换tab")
    changeTab(tkey) {
        this.selectedKey = tkey;
    }

    @action
    setLogId(logId = -1) {
        this.logId = logId;
    }

    @action
    setCommonParam(commonParam = {}) {
        this.commonParam = commonParam;
    }

    @action
    setCheckResult(logId = -1, result = -1) {        //result  -1： 未验证 0： 验证失败 1：验证成功
        if (logId > 0) {
            this.checkResult.set(logId, result);
        }
    }

    @action
    setIsProtected(logId = -1, isProtected = 0) {
        if (logId > 0) {
            this.isProtected.set(logId, isProtected);
        }
    }

    @action
    setProtectType(logId = -1, protectType = 0) {
        if (logId > 0) {
            this.protectTypes.set(logId, protectType);
        }
    }
}